
#include <algorithm>
#include "deformationMesh.h"

DeformationMesh::DeformationMesh() {
   
   model = 0;
   rbf = 0;
}

DeformationMesh::DeformationMesh(Mesh *m) {
   
   assert(m != 0);
   model = m;
   rbf = 0;
}

DeformationMesh::~DeformationMesh() {
   
   if (rbf != 0) delete rbf;
}

void DeformationMesh::setMesh(Mesh *m) {
   
   assert(m != 0);
   model = m;
}

void DeformationMesh::setting(const Constraints *constraints) {
   
   assert(model != 0);
   
   ipositions.resize(model->nvertices());
   inormals.resize(model->nvertices());
   Vertex_iterator vi = model->vertices_begin();
   for (unsigned int i=0; i<ipositions.size(); ++i, ++vi) {
      ipositions[i] = vi->point();
      inormals[i] = vi->normal();
   }
   
   cout<<"opaaaaaaaaaaa"<<endl;
   if (rbf != 0) delete rbf;
   rbf = new RBFfunction;
   rbf->setting(constraints);
   cout<<"opaaaaaaaaaaa rbf"<<endl;
}

void DeformationMesh::execute_deformation(const Constraints *constraints) {
   
   rbf->compute_lambdas(constraints);
   Vertex_iterator vi = model->vertices_begin();
   int i=0;
   while (vi != model->vertices_end()) {
      Point3 &p = vi->point();
      p = rbf->evalf(ipositions[i]);
      ++vi; ++i;
   }
}
